var member;
var agentGrade=0;
$(function(){
	member=getEntity("memberInfo");
	/***2017.7.15变更，【便于实时查询代理商信息】auto:xiongyong***********************************/
	if(member.agentGrade){
		agentGrade=member.agentGrade;
	}
	$.ajax({
		type:"post",
		url:rootPath+"/member/selectAllGrade",
		data:{'agentGrade':agentGrade},
			success:function(data){
			if(data.resultSuccess){
				var memberData = data.memberDto;
				if(memberData){
					queryMember(memberData);
				}
				var agentGradeList=data.data;
				var str='';
				$.each(agentGradeList,function(i,item){
					//<option>--请选择--</option>
						if(i==0){
							$('.agentApp li span').text(item.gradeName);
							$("#agentGrade").val(item.grade);
						}
						str+='<li agentGrade='+item.grade+'>'+item.gradeName+'</li>';
				});
				$("#agentGradeSelected").empty();
				$("#agentGradeSelected").append(str);
			}
		}
	});
	
});

	/***获取对象的值******************/
	function queryMember(member){
		if(member.companyName){
			$("#companyName").val(member.companyName);
		}
		if(member.linkman){
			$("#linkman").val(member.linkman);
		}
		if(member.phone){
			$("#phone").val(member.phone);
		}
		if(member.mId){
			$("#mId").val(member.mId);
		}
		if(member.businessLicence!=null && member.businessLicence!=''){
			$(".js_uploadBox a").html("<img id='businessLicence' src='"+baseFile+""+member.businessLicence+"' alt='营业执照'  class='pd'>");
		}else{
			$(".js_uploadBox a").html('<img src="'+rootPath+'/image/upload1.png" alt="营业执照" id="businessLicence" class="pd">');
		}
	}

	//申请代理商
	$('.agentApp li span').click(function(){
		$('.agentApp li ul').css({display:'block'});
	});
	$(document).on('click','.agentApp li ul li',function(e){
		$('.agentApp li ul li').css({color:'rgb(51,51,51)'})
		$(this).parent('ul').css({display: 'none'});
		$(this).css({color:'rgb(202,14,84)'});
		var value1 = $(this).text();
		var agentGrade1=$(this).attr("agentgrade");
		$('.agentApp li span').text(value1);	
		$("#agentGrade").val(agentGrade1);
	});
	
	/*********
	 *  提交信息增加校验提示
	 *  
	 * ***************/
	$(".btn").click(function(){
		var mId=$("#mId").val();
		var companyName=$("#companyName").val();
		var phone=$("#phone").val();
		var linkman=$("#linkman").val();
		var agentGrade=$("#agentGrade").val();
		if(companyName==null || companyName==''){
			alert("公司名称不能为空！")
			return false;
		}
		if(linkman==null || linkman==''){
			alert("联系人不能为空！")
			return false;
		}
		if(phone==null || phone==""){
			alert("手机号码不能为空！！！")
			return false;
		}
		if(phone.length!=11) { 
			alert("请输入有效的11 位手机号码！！")
			return false;
		} 
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0]{9})|(18[0-9]{1}))+\d{8})$/; 
		if (!myreg.test(phone)) { 
			alert("手机号码格式不正确!!!")
			return false;
		}
		
		$.ajaxFileUpload({
			  url:rootPath+"/member/insertAgent",
			  type:'post',
			  data:{'mId':mId,'companyName':companyName,'phone':phone,'linkman':linkman,'agentGrade':agentGrade},
		      secureuri:false,  
		      fileElementId:"agentImg",//文件选择框的id属性  
		      dataType: 'JSON',   //json 
		      success: function (data) {
		    	var reg = /<pre.+?>(.+)<\/pre>/g;  
		    	var result = data.match(reg);  
		    	data = RegExp.$1;
		    	data=toJson(data);
		    	if(data.resultSuccess){
					alert(data.msg);
		    	}else{
		    		alert(data.resultDesc);
		    	}
		     }
		});
	});
	function setImagePreview() { 
		var docObj=document.getElementById("agentImg"); 
		var imgObjPreviewBig=document.getElementById("businessLicence"); 
		if(docObj.files && docObj.files[0]){ 
			//火狐下，直接设img属性
		//	imgObjPreview.style.display = 'block'; 
			imgObjPreviewBig.style.width = '30%'; 
			imgObjPreviewBig.style.height = '30%'; 
			//imgObjPreview.style.margin-left='18px'; 
			//imgObjPreview.src = docObj.files[0].getAsDataURL(); 
			//火狐7以上版本不能用上面的getAsDataURL()方式获取，需要一下方式
			imgObjPreviewBig.src = window.URL.createObjectURL(docObj.files[0]); 
		}else{ 
		//IE下，使用滤镜
			docObj.select(); 
			var imgSrc = document.selection.createRange().text; 
			var localImagId = document.getElementById("localImag"); 
			//必须设置初始大小
			localImagId.style.width = "30%"; 
			localImagId.style.height = "30%"; 
		//图片异常的捕捉，防止用户修改后缀来伪造图片
			try{ 
				localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; 
				localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; 
			}catch(e){ 
				alert("您上传的图片格式不正确，请重新选择!"); 
				return false;  
			}
		}
	}	